package nc.impl.so.m32;

import java.util.ArrayList;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.data.bill.BillInsert;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.impl.pubapp.pattern.data.vo.VODelete;
import nc.impl.pubapp.pattern.data.vo.VOInsert;
import nc.impl.pubapp.pattern.data.vo.VOQuery;
import nc.itf.so.m32.profit.IFindPriceBySaleID;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.ui.price.priceform.model.PriceFormVOChangeUtils;
import nc.vo.price.pub.entity.PriceFormAggVO;
import nc.vo.price.pub.entity.PriceFormBVO;
import nc.vo.price.pub.entity.PriceFormTempAggVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.so.invoiceprice.InvoicepriceAggVO;
import nc.vo.so.invoiceprice.InvoicepriceBVO;
import nc.vo.so.invoiceprice.InvoicepriceHVO;
import nc.vo.so.m30.entity.SaleOrderBVO;

public class FindPriceBySaleIDImpl implements IFindPriceBySaleID {

	@Override
	public PriceFormAggVO queryPricevoBySalePk(String saleId,String firstPK) {
		// TODO Auto-generated method stub

		try {
			String pk = findPriceFormId(saleId,firstPK);
			if (pk == null || pk.equals("")) {
				return null;
			}
			PriceFormAggVO vo = queryPriceFormByPK(pk);
			return vo;
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public String findPriceFormId(String saleId,String firstPK) {
		// TODO Auto-generated method stub
		String sql = "select cpriceformid from so_saleorder_b where csaleorderbid='"
				+ saleId + "'";
		
		String sql2 = "select cpriceformid from so_saleorder_b where csaleorderbid='"
			+ firstPK + "'";
		IUAPQueryBS iq = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		String str;
		try {
			str = (String) iq.executeQuery(sql, new ColumnProcessor());
		if(str==null){
			str = (String) iq.executeQuery(sql2, new ColumnProcessor());
		}	
			return str;
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public InvoicepriceAggVO findPriveVoByPk(String pk) {
		// TODO Auto-generated method stub

				IUAPQueryBS iq = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		String sqlh = "select * from so_invoicepricehvo where pk_priceform='"
				+ pk + "'";
		String sqlb = "select * from so_invoicepricebvo where pk_priceform='"
				+ pk + "'";
		InvoicepriceAggVO aggVO = new InvoicepriceAggVO();

		try {
			InvoicepriceHVO hvo = (InvoicepriceHVO) iq.executeQuery(sqlh,
					new BeanProcessor(InvoicepriceHVO.class));

			ArrayList<InvoicepriceBVO> list = (ArrayList<InvoicepriceBVO>) iq
					.executeQuery(sqlb, new BeanListProcessor(
							InvoicepriceBVO.class));

			if (hvo == null) {
				return null;
			}
			aggVO.setParentVO(hvo);
			if (list.size() > 0) {
			}
			{
				InvoicepriceBVO[] bvos = list.toArray(new InvoicepriceBVO[1]);
				aggVO.setChildrenVO(bvos);
			}
			return aggVO;
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}		 

	}



	@Override
	public SaleOrderBVO querySaleOrderBVO(String pk) {
		// TODO Auto-generated method stub
		IUAPQueryBS iq = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		String sql = " select * from so_saleorder_b  ssb where ssb.cpriceformid='" + pk + "'";
		
		try {
			SaleOrderBVO sbVO = (SaleOrderBVO) iq.executeQuery(sql,
					new BeanProcessor(SaleOrderBVO.class));

			return sbVO;
			
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	
	}

	@Override
	public InvoicepriceAggVO SaveVO(InvoicepriceAggVO vo) {
		// TODO Auto-generated method stub
		return new BillInsert<InvoicepriceAggVO>()
				.insert(new InvoicepriceAggVO[] { vo })[0];
	}

	@Override
	public void SaveBVO(InvoicepriceBVO[] bvos) {
		// TODO Auto-generated method stub
		new VOInsert<InvoicepriceBVO>().insert(bvos);
	}

	@Override
	public boolean HasePrice(String pk) {
		// TODO Auto-generated method stub
		String sql = "select count(*) from so_invoicepricehvo where pk_priceform='"
				+ pk + "'";
		IUAPQueryBS iq = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		try {
			int num = (Integer) iq.executeQuery(sql, new ColumnProcessor());
			if (num == 1) {
				return true;
			}
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public PriceFormAggVO queryPriceFormByPK(String pk_priceForm)
			throws BusinessException {
		PriceFormAggVO aggVO = new PriceFormAggVO();

		try {
			PriceFormTempAggVO[] tempAggVOs = new BillQuery<PriceFormTempAggVO>(
					PriceFormTempAggVO.class)
					.query(new String[] { pk_priceForm });
			if (tempAggVOs != null && tempAggVOs.length > 0) {
				if (tempAggVOs[0] != null) {
					aggVO = PriceFormVOChangeUtils
							.changePriceFormTempAggVO(tempAggVOs)[0];
				}
			} else {
				PriceFormAggVO[] aggVOs = new BillQuery<PriceFormAggVO>(
						PriceFormAggVO.class)
						.query(new String[] { pk_priceForm });
				if (aggVOs == null || aggVOs.length == 0) {
					return null;
				}
				aggVO = aggVOs[0];
			}
			PriceFormBVO[] bvos = PriceFormVOChangeUtils
					.showPriceFormBVOs(aggVO.getChildrenVO());
			aggVO.setChildrenVO(bvos);
			return aggVO;
		} catch (Exception e) {
			ExceptionUtils.marsh(e);
		}
		return null;
	}

	@Override
	public void DropVO(String pk) {
		// TODO Auto-generated method stub
		BaseDAO dao = new BaseDAO();
		String sql = "delete from so_invoicepricehvo where pk_priceform='" + pk
				+ "'";
		String sql2 = "delete from so_invoicepricebvo where pk_priceform='"
				+ pk + "'";
		if (HasePrice(pk)) {
			try {
				dao.executeUpdate(sql2);
				dao.executeUpdate(sql);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}


}
